package com.xinyue.network.message.game;

import java.io.UnsupportedEncodingException;

import com.alibaba.fastjson.JSON;
import com.xinyue.network.message.error.IServerError;

import io.netty.buffer.ByteBuf;

public class GameMessageHeader implements Cloneable {
    public static int HEADER_LENGTH = 25;
    public static int HEADER_INNER_LENGTH = 53;
    private int messageSize;
    private int messageId;
    private int serviceId;
    private long clientSendTime;
    private long serverSendTime;
    private int clientSeqId;
    private int version;
    private int errorCode;
    private int fromServerId;
    private int toServerId;
    private long playerId;
    private boolean compress;
    private EnumMesasageType mesasageType;
    
    private String desc;

    private HeaderAttribute attribute = new HeaderAttribute();

    @Override
    public GameMessageHeader clone() throws CloneNotSupportedException {
        GameMessageHeader newHeader = (GameMessageHeader) super.clone();
        return newHeader;
    }

    public void readRequestHeader(ByteBuf byteBuf) {
        messageSize = byteBuf.readInt();
        clientSeqId = byteBuf.readInt();
        messageId = byteBuf.readInt();
        serviceId = byteBuf.readInt();
        int type = byteBuf.readByte();
        mesasageType = EnumMesasageType.getType(type);
        clientSendTime = byteBuf.readLong();
        version = byteBuf.readInt();
        if (byteBuf.readableBytes() > 0) {
            int compress = byteBuf.readByte();
            this.compress = compress == 1 ? true : false;
        }
    }

    public void writeResponseHeader(ByteBuf out) {
        out.writeByte(this.getMesasageType().getType());
        out.writeInt(this.getClientSeqId());
        out.writeInt(this.getMessageId());
        out.writeLong(this.getServerSendTime());
        out.writeInt(this.getVersion());
        out.writeInt(this.getErrorCode());
    }
    
    public void readInnerHeader(ByteBuf byteBuf) throws UnsupportedEncodingException {
    	 messageSize = byteBuf.readInt();//依次读取包头信息
         toServerId = byteBuf.readInt();
         fromServerId = byteBuf.readInt();
         clientSeqId = byteBuf.readInt();
         messageId = byteBuf.readInt();
         serviceId = byteBuf.readInt();
         int  type = byteBuf.readByte();
         mesasageType = EnumMesasageType.getType(type);
         version = byteBuf.readInt();
         clientSendTime = byteBuf.readLong();
         serverSendTime = byteBuf.readLong();
         playerId = byteBuf.readLong();
         errorCode = byteBuf.readInt();
         int attLeng = byteBuf.readInt();
         byte[] attrBytes = new byte[attLeng];
         byteBuf.readBytes(attrBytes);
         String attrJson = new String(attrBytes, "utf8");
         this.attribute = JSON.parseObject(attrJson,HeaderAttribute.class);
        
    }
    
    public void writeInnerHeader(ByteBuf byteBuf)  {
    	byteBuf.writeInt(this.getToServerId());
        byteBuf.writeInt(this.getFromServerId());
        byteBuf.writeInt(this.getClientSeqId());
        byteBuf.writeInt(this.getMessageId());
        byteBuf.writeInt(this.getServiceId());
        byteBuf.writeByte(this.getMesasageType().getType());
        byteBuf.writeInt(this.getVersion());
        byteBuf.writeLong(this.getClientSendTime());
        byteBuf.writeLong(this.getServerSendTime());
        byteBuf.writeLong(this.getPlayerId());
        byteBuf.writeInt(this.getErrorCode());
    }
    
    public byte[] getHeaderAttributeBytes() throws UnsupportedEncodingException {
    	String attrJson = JSON.toJSONString(attribute);
        byte[] attrBytes = attrJson.getBytes("utf8");
        return attrBytes;
    }

    
    public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

	public EnumMesasageType getMesasageType() {
        return mesasageType;
    }

    public void setMesasageType(EnumMesasageType mesasageType) {
        this.mesasageType = mesasageType;
    }

    public long getPlayerId() {
        return playerId;
    }

    public void setPlayerId(long playerId) {
        this.playerId = playerId;
    }

    public HeaderAttribute getAttribute() {
        return attribute;
    }

    public void setAttribute(HeaderAttribute attribute) {
        this.attribute = attribute;
    }

    public int getFromServerId() {
        return fromServerId;
    }

    public void setFromServerId(int fromServerId) {
        this.fromServerId = fromServerId;
    }

    public int getToServerId() {
        return toServerId;
    }

    public void setToServerId(int toServerId) {
        this.toServerId = toServerId;
    }

    public int getServiceId() {
        return serviceId;
    }

    public void setServiceId(int serviceId) {
        this.serviceId = serviceId;
    }

    public int getMessageSize() {
        return messageSize;
    }

    public void setMessageSize(int messageSize) {
        this.messageSize = messageSize;
    }

    public int getErrorCode() {
        return errorCode;
    }
    public void errorCode(IServerError serverError) {
    	this.errorCode = serverError.getErrorCode();
    }

    public void setErrorCode(int errorCode) {
        this.errorCode = errorCode;
    }

    public long getClientSendTime() {
        return clientSendTime;
    }

    public void setClientSendTime(long clientSendTime) {
        this.clientSendTime = clientSendTime;
    }

    public long getServerSendTime() {
        return serverSendTime;
    }

    public void setServerSendTime(long serverSendTime) {
        this.serverSendTime = serverSendTime;
    }


    public int getClientSeqId() {
        return clientSeqId;
    }

    public void setClientSeqId(int clientSeqId) {
        this.clientSeqId = clientSeqId;
    }



    public int getVersion() {
        return version;
    }

    public void setVersion(int version) {
        this.version = version;
    }

    public int getMessageId() {
        return messageId;
    }

    public void setMessageId(int messageId) {
        this.messageId = messageId;
    }



    public boolean isCompress() {
        return compress;
    }

    public void setCompress(boolean compress) {
        this.compress = compress;
    }
    

    @Override
    public String toString() {
        return desc + " [" + this.clientSeqId + "]" + " : GameMessageHeader [messageSize=" + messageSize + ", messageId=" + messageId + ", serviceId=" + serviceId + ", clientSendTime=" + clientSendTime + ", serverSendTime=" + serverSendTime + ", clientSeqId=" + clientSeqId + ", version=" + version + ", errorCode=" + errorCode + ", fromServerId=" + fromServerId + ", toServerId=" + toServerId + ", playerId=" + playerId + ", attribute=" + attribute + "]";
    }

}
